﻿using Service.CertWorkFlow.Steps;
using WorkflowCore.Exceptions;
using WorkflowCore.Interface;

namespace Service.CertWorkFlow
{
    public class BuildCertWorkFlow : IWorkflow<WorkflowData>
    {
        public string Id => "CertWorkFlow";
        public int Version => 1;

        public void Build(IWorkflowBuilder<WorkflowData> builder)
        {
            builder
                .StartWith<SendCertOrder>()
                .OnError(WorkflowCore.Models.WorkflowErrorHandling.Terminate)
                .Then<EditDNSRecord>()
                .OnError(WorkflowCore.Models.WorkflowErrorHandling.Terminate)
                .Then<ValidateDnsTxtRecord>()
                .OnError(WorkflowCore.Models.WorkflowErrorHandling.Terminate)
                .Then<GetCert>()
                .OnError(WorkflowCore.Models.WorkflowErrorHandling.Terminate)
                .ForEach(context=>context.Config.CertHandOutConfig,data=>false)
                .Do(x=>x.StartWith<CertHandOut>()
                    .Input(step=>step.handOutConfig, (data, context) => context.Item)
                )
                .Then<End>();
        }
    }
}
